package com.opdar.athena.client.agent.server;

import com.opdar.athena.base.Command;
import com.opdar.athena.base.Constants;
import com.opdar.athena.client.agent.base.Configuration;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 功   能:
 * 创建者: 施俊帆
 * 日   期: 2014/7/23 16:21
 * Q  Q: 362116120
 */
public class AthenaServer {
    private int port;
    private ChannelFuture channelFuture;
    public static final ConcurrentHashMap<String, IoSession> sessions = new ConcurrentHashMap<String, IoSession>();
    private EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    private EventLoopGroup workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() + 1);
    private boolean isStoping = false;

    public AthenaServer() {
        this.port = Configuration.getConfiguration().getPort();
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Configuration.getConfiguration().setContext(context);
    }

    public void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .childHandler(new AthenaInitializer());
                try {
                    channelFuture = b.bind(port);
                    channelFuture = channelFuture.sync();
                    channelFuture = channelFuture.channel().closeFuture();
                    channelFuture = channelFuture.sync();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    stop();
                }
            }
        }).start();
    }

    public synchronized void stop() {
        if (!isStoping) {
            isStoping = true;
            try {
                workerGroup.shutdownGracefully().sync();
                bossGroup.shutdownGracefully().sync();
                channelFuture.channel().close();
                channelFuture.cancel(true);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Map<String, String> values = Command.valueOf(args);
        if(values.containsKey("--config")){
            Constants.GLOBAL_PROPERTIES = values.get("--config");
        }
        if(values.containsKey("--port")){
            String port = values.get("--port");
            if(!StringUtils.isEmpty(port)){
                int serverPort = Integer.valueOf(port);
                Configuration.getConfiguration().setPort(serverPort);
            }
        }
        AthenaServer server = new AthenaServer();
        server.start();
    }

}
